import 'package:json_annotation/json_annotation.dart';
import 'package:ileddemo/code/ble/protocol/base/SizeConverter.dart';
import 'package:ileddemo/code/ble/protocol/base/SourceType.dart';
import 'package:ileddemo/code/ble/protocol/base/RectConverter.dart';
import 'package:lib_util/CrcUtils.dart';
import '../ColorSourceInterface.dart';
import '../SourceManager.dart';

part 'PartitionSourceBean.g.dart';

//分区素材
@JsonSerializable()
class PartitionSourceBean extends ColorSourceInterface {
  //分区数据对应的id
  List<int> ids = [];

  @JsonKey(ignore: true)
  List<ColorSourceInterface> sources = [];

  //分区类型枚举
  int partitionType = 0;

  PartitionSourceBean() {
    sourceType = SourceType.TYPE_PARTITION;
  }

  //获取分区总个数
  int getPartitionCount() {
    int count = 0;
    return count;
  }

  ColorSourceInterface getSourceForId(int id) {
    ColorSourceInterface? source;

    for (var s in sources) {
      if (s.id == id) {
        source = s;
        break;
      }
    }
    source ??= SourceManager.instance.getSourceForId(id) as ColorSourceInterface;
    return source;
  }

  @override
  Future<List<int>> getSendData(bleBean) async {
    List<int> data = [];
    //总个数
    data.add(1);
    //预留
    data.addAll([0, 0, 0]);
    //分区内容存储位置
    data.addAll([0, 0, 0, 0]);
    data.addAll([0, 0, 0, 0]);
    data.addAll([0, 0, 0, 0]);
    data.addAll([0, 0, 0, 0]);

    //具体对的节目内容

    var sourceBean = getSourceForId(ids[0]);
    var d = await sourceBean.getSendData(bleBean);
    data.addAll(sourceBean.getProgramParameters(bleBean) + d);

    //计算整条节目的id
    var id = CrcUtils.getCRC32(data);
    return id + data;
  }

  factory PartitionSourceBean.fromJson(Map<String, dynamic> json) => _$PartitionSourceBeanFromJson(json);

  @override
  Map<String, dynamic> toJson() => _$PartitionSourceBeanToJson(this);
}
